package com.ipet.app.adapter.ViewHolder;

import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ipet.app.R;
import com.ipet.app.adapter.ViewMulImageAdapter;
import com.ipet.app.api.BaseCallBack;
import com.ipet.app.api.RequestManager;
import com.ipet.app.bean.HotPost;
import com.ipet.app.bean.apiRequestBean.AgreePostRequest;
import com.ipet.app.bean.apiRequestBean.CancelAgreePostRequest;
import com.ipet.app.bean.apiResponseBean.BaseResponse;
import com.ipet.app.ui.activity.MainActivity;
import com.ipet.app.ui.activity.PostDetailActivity;
import com.ipet.app.ui.common.PhotoViewActivity;
import com.ipet.app.util.ImageUtil;
import com.ipet.app.util.SpaceItemDecoration;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

/**
 * Created by jindongping on 16/3/18.
 */
public class PostHotViewHolderImg extends BaseViewHolder<HotPost> {
    LinearLayout lineAgree, lineReply;
    CardView cardView;
    TextView name, time, title, content;
    RecyclerView imageRecyclerView;
    TextView agreeIc, replyIc, agreeTv;

    public PostHotViewHolderImg(ViewGroup parent, @LayoutRes int res) {
        super(parent, res);
        lineAgree = $(R.id.line_agree);
        lineReply = $(R.id.line_reply);
        cardView = $(R.id.card_view);
        name = $(R.id.post_name);
        time = $(R.id.post_time);
        title = $(R.id.post_title);
        content = $(R.id.post_content);
        imageRecyclerView = $(R.id.post_image_recyclerView);
        agreeIc = $(R.id.ic_agree);
        replyIc = $(R.id.ic_reply);
        agreeTv = $(R.id.tv_agree);
    }

    SpaceItemDecoration spaceItemDecoration =new SpaceItemDecoration(0,3);

    @Override
    public void setData(final HotPost data) {
        super.setData(data);
        //初始化数据
        name.setText(data.getNickname());
        time.setText(data.getCreate_time());
        if (data.getTheme() != null && data.getTheme().length() > 1) {
            title.setVisibility(View.VISIBLE);
            title.setText(data.getTheme());
        }
        content.setText(data.getContent());

        //设置 多图显示
        final String[] images = new Gson().fromJson(data.getPicurl(), String[].class);
        ViewMulImageAdapter adapter = new ViewMulImageAdapter(getContext());
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getContext(), PhotoViewActivity.class);
                intent.putExtra("images", images);
                intent.putExtra("position", position);
                getContext().startActivity(intent);
            }
        });
        if (images.length >= 3) {
            adapter.addAll(ImageUtil.getInstance(getContext()).convertUrlToDisplayImg(images,10));
            imageRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        } else {
            adapter.addAll(ImageUtil.getInstance(getContext()).convertUrlToDisplayImg(images,20));
            imageRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), images.length));
        }
        imageRecyclerView.removeItemDecoration(spaceItemDecoration);
        imageRecyclerView.addItemDecoration(spaceItemDecoration);
        imageRecyclerView.setAdapter(adapter);

        //设置点赞
        AgreeListener agreeListener = new AgreeListener(data,agreeTv,agreeIc,lineAgree);
        CancelAgreeListener cancelAgreeListener = new CancelAgreeListener(data,agreeTv,agreeIc,lineAgree);
        agreeListener.setCancelAgreeListener(cancelAgreeListener);
        cancelAgreeListener.setAgreeListener(agreeListener);
        if (data.getIsAgree() == null) {
            //没有点赞
            agreeIc.setBackgroundResource(R.mipmap.ic_agree);
            agreeTv.setText("" + data.getAgree_num());
            lineAgree.setOnClickListener(agreeListener);
        } else {
            //已经点赞了
            agreeIc.setBackgroundResource(R.mipmap.ic_disagree);
            lineAgree.setOnClickListener(cancelAgreeListener);
            agreeTv.setText("" + data.getAgree_num());
        }

        //设置评论按钮跳转
        lineReply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PostDetailActivity.class);
                intent.putExtra("hotPost", data);
                getContext().startActivity(intent);
            }
        });
        //设置cardView跳转
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PostDetailActivity.class);
                intent.putExtra("hotPost", data);
                getContext().startActivity(intent);
            }
        });
    }

    public static class AgreeListener implements View.OnClickListener {
        private HotPost data;
        private CancelAgreeListener cancelAgreeListener;
        private TextView agreeTv, agreeIc;
        private LinearLayout lineAgree;

        public AgreeListener(HotPost data, TextView agreeTv, TextView agreeIc, LinearLayout lineAgree) {
            this.data = data;
            this.agreeTv = agreeTv;
            this.agreeIc = agreeIc;
            this.lineAgree = lineAgree;
        }

        public void setCancelAgreeListener(CancelAgreeListener cancelAgreeListener) {
            this.cancelAgreeListener = cancelAgreeListener;
        }

        @Override
        public void onClick(View v) {
            RequestManager.getApi().agreePost(new AgreePostRequest(data.getActive_id())).enqueue(new BaseCallBack<BaseResponse>(MainActivity.fabMenu) {
                @Override
                public void onRequestSuccess(BaseResponse response) {
                    data.setAgree_num((Integer.parseInt(data.getAgree_num()) + 1) + "");
                    agreeTv.setText("" + (Integer.parseInt(data.getAgree_num())));
                    agreeIc.setBackgroundResource(R.mipmap.ic_disagree);
                    lineAgree.setOnClickListener(cancelAgreeListener);
                    data.setIsAgree("已赞不为空");
                }
            });
        }
    }

    public static class CancelAgreeListener implements View.OnClickListener {

        private HotPost data;
        private AgreeListener agreeListener;
        private TextView agreeTv, agreeIc;
        private LinearLayout lineAgree;

        public CancelAgreeListener(HotPost data, TextView agreeTv, TextView agreeIc, LinearLayout lineAgree) {
            this.data = data;
            this.agreeTv = agreeTv;
            this.agreeIc = agreeIc;
            this.lineAgree = lineAgree;
        }

        public void setAgreeListener(AgreeListener agreeListener) {
            this.agreeListener = agreeListener;
        }

        @Override
        public void onClick(View v) {
            RequestManager.getApi().cancelAgreePost(new CancelAgreePostRequest(data.getActive_id())).enqueue(new BaseCallBack<BaseResponse>(MainActivity.fabMenu) {
                @Override
                public void onRequestSuccess(BaseResponse response) {
                    data.setAgree_num((Integer.parseInt(data.getAgree_num()) - 1) + "");
                    agreeTv.setText("" + (Integer.parseInt(data.getAgree_num())));
                    lineAgree.setOnClickListener(agreeListener);
                    agreeIc.setBackgroundResource(R.mipmap.ic_agree);
                    data.setIsAgree(null);
                }
            });
        }
    }


}